package service4gateway

import (
	UserAction "MyBlogv2/Servers/UserAction/userAction"
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"MyBlogv2/blog-common/response"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"strconv"
)

func AddComment(c *gin.Context) {
	var userCommentDto UserAction.UserCommentDto
	userId, _ := c.Get("userId")
	if err := c.ShouldBind(&userCommentDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	rsp, _ := clientOfUserAction.AddComment(c.Request.Context(), &UserAction.UserActionRequest{
		UserCommentDto: &userCommentDto,
		UserId:         int64(userId.(int)),
	})
	response.CommonResponse(c, HttpCode.AppHttpCodeEnum(rsp.Code), rsp.Data, rsp.Msg)
	return
}

func GetCommentList(c *gin.Context) {
	typeOfComment, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	pageNum, _ := strconv.Atoi(c.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	rsp, _ := clientOfUserAction.GetCommentList(c.Request.Context(), &UserAction.UserActionRequest{
		Type:     int64(typeOfComment),
		TypeId:   int64(typeId),
		PageNum:  int64(pageNum),
		PageSize: int64(pageSize),
	})
	var commentVoList []models.ArticleCommentVo
	commentVoList = CopyWithChildren(rsp.Data)
	response.ResponseOK(c, gin.H{
		"total": rsp.Total,
		"page":  commentVoList,
	})

}
func CopyWithChildren(commentVoList []*UserAction.ArticleCommentVo) []models.ArticleCommentVo {
	var commentVoListResponse []models.ArticleCommentVo
	copier.Copy(&commentVoListResponse, &commentVoList)
	for i, v := range commentVoList {
		commentVoListResponse[i].CreateTime = helper.TimestampToMyTime(v.CreateTime)
		commentVoListResponse[i].ChildComment = CopyWithChildren(v.ChildComment)
	}
	return commentVoListResponse
}

func GetCommentList4Admin(c *gin.Context) {
	var commentVoList []models.CommentListVo
	models.Db.Model(new(models.CommentListVo)).
		Table("t_comment c").
		Select("c.*,u.username as comment_username").
		Joins("left join sys_user u on c.comment_user_id = u.id").
		Find(&commentVoList)
	response.ResponseOK(c, commentVoList)
	return
}

func SearchComment(c *gin.Context) {
	var searchCommentDto models.SearchCommentDto
	if err := c.ShouldBind(&searchCommentDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	db := models.Db.Model(new(models.CommentListVo)).
		Table("t_comment c").
		Select("c.*,u.username as comment_username").
		Joins("left join sys_user u on c.comment_user_id = u.id")
	if searchCommentDto.CommentContent != "" {
		db.Where("comment_content like ?", "%"+searchCommentDto.CommentContent+"%")
	}
	if searchCommentDto.CommentUserName != "" {
		db.Where("u.username like ?", "%"+searchCommentDto.CommentUserName+"%")
	}
	if searchCommentDto.Type.Valid {
		db.Where("type = ?", searchCommentDto.Type.Int64)
	}
	if searchCommentDto.IsCheck.Valid {
		db.Where("is_check = ?", searchCommentDto.IsCheck.Int64)
	}
	var commentVoList []models.CommentListVo
	db.Find(&commentVoList)
	response.ResponseOK(c, commentVoList)
}

func UpdateCommentIsCheck(c *gin.Context) {
	var commentIsCheckDto models.CommentIsCheckDto
	if err := c.ShouldBind(&commentIsCheckDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	models.Db.Model(new(models.Comment)).Where("id = ?", commentIsCheckDto.Id).Update("is_check", commentIsCheckDto.IsCheck)

	models.Db.Model(new(models.Comment)).Where("parent_id = ?", commentIsCheckDto.Id).Update("is_check", commentIsCheckDto.IsCheck)
	response.ResponseOK(c, nil)
	return
}

func DeleteCommentById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	models.Db.Model(new(models.Comment)).Where("id = ?", id).Or("parent_id = ?", id).Delete(new(models.Comment))
	response.ResponseOK(c, nil)
}
